<?php


namespace app\api\controller\xiluedu;


use addons\xiluedu\controller\WeixinMini;
use app\common\controller\Api;
use app\common\model\xiluedu\Banner AS BannerModel;
use OSS\OssClient;

class Common extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 图片上传
     */
    public function params()
    {
        $oss = get_addon_config('alioss');

        if(!$oss){
            $this->error('未安装oss插件');
        }
        $name = $this->request->post('name');
        $md5 = $this->request->post('md5');
        $auth = new \addons\alioss\library\Auth();
        $params = $auth->params($name, $md5);
        config('default_return_type','json');//返回json而不是html
        $this->success('', $params);
    }

    /**小程序二维码图片上传
     * @param $filePath
     * @param $fileData
     * @return bool
     */
    public static function uploadAlioss($filePath, $fileData) {
        $config = get_addon_config('alioss');
        if(empty($config) || !isset($config['accessKeyId']))  return true;
        $accessKeyId = $config['accessKeyId'];
        $accessKeySecret = $config['accessKeySecret'];
        $endpoint = $config['endpoint'];
        $bucket = $config['bucket'];
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $ossClient->putObject($bucket, ltrim($filePath, '/'),$fileData);
        return true;
    }

}